<template>
  <div class="my-container">
    <Header title="个人中心" :is-arrow="true" back-color="#f1f4f9" />
    <van-cell-group v-if="userInfo !== null" class="my-info">
      <van-cell
        class="base-info"
        center
        :border="false"
      >
        <van-image
          class="avatar"
          slot="icon"
          round
          fit="cover"
          :src="'http://123.56.158.143:8250/getImgStream?imgPath=' + userInfo.user.avatar"
        />
        <div class="name" slot="title">{{ userInfo.user.username }}</div>
        <van-button
          class="update-btn"
          size="small"
          round
        >编辑资料
        </van-button>
      </van-cell>
    </van-cell-group>

    <div v-else class="not-login">
      <div @click="$router.push('/login')">
        <van-icon class="user-mobile" name="user-circle-o" size="50" color="#ffffff"/>
      </div>
      <div class="text">登录 / 注册</div>
    </div>

    <van-cell title="消息通知" is-link to="/user/message" />
    <van-cell title="我的订单" is-link :to="$store.state.user === null ? '/login' : '/user/order'" />
    <van-cell title="我的券包" is-link :to="$store.state.user === null ? '/login' : '/user/ticket'" />
    <van-cell title="我的钱包" is-link :to="$store.state.user === null ? '/login' : '/wallet'" />

    <van-cell
      v-if="userInfo !== null"
      class="logout-cell"
      title="退出登录"
      @click="onLogout"
    />
  </div>
</template>

<script>
import Header from '@/components/basic/Header'
// 登陆成功了以后 Base State里放了一个user数据
// 只要判断有没有这个user就能判断用户有没有登录
// 为了使用容器里的数据

// import axios from 'axios'
export default {
  name: 'MyIndex',
  components: {
    Header
  },
  props: {},
  data () {
    return {
      currentUser: {} // 当前用户登录信息
    }
  },
  computed: {
    // 映射到组件当中
    // 将容器里的数据映射到本地里
    userInfo () {
      return this.$store.state.user
    }
  },
  watch: {},
  mounted () {
    console.log(this.userInfo.user)
    this.loadCurrentUser()
  },
  methods: {
    loadCurrentUser () {
      // axios.get(`/sys/vip/${this.userInfo.id}`).then(({ data }) => {
      //   console.log(data)
      //   this.currentUser = { ...data.data }
      //   console.log('@@@@', this.currentUser)
      // })
    },
    onLogout () {
      // 提示用户确认退出
      // 确认 -> 处理退出
      this.$dialog.confirm({
        title: '退出提示',
        message: '确认退出吗'
      }).then(() => { // 确认执行这里
        // 清除用户登陆状态
        this.$store.commit('setUser', null)
      }).catch(() => {
        // on cancel
      })
    }
  }
}
</script>

<style scoped lang="scss">
.my-container {
  .my-info {
    background: url("./1.jpg") no-repeat;
    background-size: cover;
    .base-info {
      box-sizing: border-box;
      height: 115px;
      background-color: unset;
      padding-top: 38px;
      padding-bottom: 11px;
      .avatar {
        box-sizing: border-box;
        height: 66px;
        width: 66px;
        border: 1px solid #FFFFFF;
        margin-right: 11px;
      }
      .name {
        font-size: 20px;
        color: #FFFFFF;
      }
      .update-btn {
        height: 16px;
        font-size: 10px;
        color: #666666;
      }
    }
    .data-info {
      .data-info-item {
        height: 65px;
        color: #FFFFFF;
        .text-wrap {
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          .count {
            font-size: 18px;
          }
          .text {
            font-size: 11px;
          }
        }
      }
    }
    .van-grid-item__content {
      background-color: unset;
    }
  }

  .not-login {
    height: 180px;
    background: url("./1.jpg") no-repeat;
    background-size: cover;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .user-mobile{
    }
    .text {
      font-size: 14px;
      color: #FFFFFF;
    }
  }

  .logout-cell {
    text-align: center;
    color: #d86262;
  }
  .mb-4 {
    margin-bottom: 4px;
  }
}
</style>
